const app = getApp()

Page({
  data: {
    hiddenmodalput:true,
    inputContent:"",
    inputTitle:"",
    avatar_url:"",
    nick: "",
    campus: "金石滩",
   student_id: 0,
    profile: "this is me",
    image_url: "",
    is_here: 0,
    status: "已认证"
  },
  onLoad: function (options) {
    var that = this;

    wx.setNavigationBarTitle({
      title: "个人信息",
    })
    var user_id = options.user_id;
    var is_here = options.is_here;
    that.setData({
      user_id: user_id,
      is_here: is_here
    })
    if(is_here == 1){
      that.onClickProfile();
    }
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '友情提示',
        content: '请先注册',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/my/my',
            })
          }
        }
      })
    }else{
      that.onGetUserMsg(user_id);
    }
  },

  onShow: function(){
    var that = this;
    var my_user_id = wx.getStorageSync('user_id');
    that.setData({
      my_user_id: my_user_id
    })
    var user_id = that.data.user_id;
    that.onGetUserMsg(user_id);
  },

  //点击昵称 修改
  onClickNick: function () {
    var that=this;
   that.setData({
      hiddenmodalput:false,
      inputContent:that.data.nick,
     inputTitle:"昵称"
    })
  },
  inputNick: function (e) {
    var that = this;
    that.setData({
      newMsg: e.detail.value
    })
  },
  confirmM: function () {
    var that = this;
    if (that.data.inputTitle=="昵称"){
      that.setData({
        nick: that.data.newMsg,
        hiddenmodalput: true
      });
      that.onChangeNick();
    }
    if (that.data.inputTitle == "修改QQ号码") {
      that.setData({
        qq: that.data.newMsg,
        hiddenmodalput: true
      })
      that.onChangeQQ();
    }
  },
  cancelM: function () {
    var that = this;
    that.setData({
      hiddenmodalput: true
    })
  },
  onClickProfile: function () {
    var that = this;
    that.setData({
      hiddenmodalput: false,
      inputContent: that.data.profile,
      inputTitle: "修改QQ号码"
    })
  },
  onGetUserMsg: function (user_id) {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'users/' + user_id + "/",
      method: 'GET',
      success: function (res) {
        var user = res.data.data;
        var username = user.student_id;
      
        var status = "已认证"
        if (username == 0){
          status = "未认证"
        }
        var campus = "金石滩";
        if(user.is_campus_type == false){
          campus = "开发区";
        }
        var qq = user.qq + '';
        if(qq.length < 5 && user_id == that.data.my_user_id){
          qq = "点此填写";
        } else if (qq.length < 5 && user_id != that.data.my_user_id){
          qq = "暂未填写";
        }
        that.setData({
          avatar_url: user.avatar_url,
          campus: campus,
          nick:user.nick,
          student_id: user.student_id,
          user: user,
          qq: qq,
          status: status
        })
      }
    })
  },

  onClickStatus: function(){
    var that = this;
    wx.navigateTo({
      url: '/pages/zhjw/bindzhjw',
    })
  },

  onChangeNick: function () {
    var that = this;
    var user_id = that.data.user_id;
    wx.request({
      url: app.globalData.targetUrl + 'users/' + user_id + '/',
      method: "PUT",
      data: {
        nick: that.data.nick
      },
      success: function (res) {
        console.log(res.data);
      }
    });
  },

  onChangeQQ: function(){
    var that = this;
    var user_id = that.data.user_id;
    console.log(that.data.qq);
    wx.request({
      url: app.globalData.targetUrl + 'users/' + user_id + '/',
      method: "PUT",
      data: {
        qq: that.data.qq
      },
      success: res => {
        console.log(res.data);
        if(res.data.error == 500){
          wx.showModal({
            title: '提示',
            content: 'qq号不存在，请重新输入',
          })
        }else{
        that.setData({
          qq: that.data.newMsg
        })
        }
      }
    })
  },

})
